<?php
/*
 * Created on 25-sept.-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class UserController extends Zend_Controller_Action
 {
    public function indexAction()
    {        
        $db = Zend::registry('db');
        $post = Zend::registry('fPost');  
        $session = Zend::registry('fSession');

        $myLogin = $post->getRaw('login');
        $myPassword = $post->getRaw('password');
        if ( ( $myLogin > "" ) && ( $myPassword > "" ) ) {
            // test dans la base de donnees //
            $sql = "SELECT * FROM users WHERE login = '" . $myLogin . "' AND password = '" . md5($myPassword) . "'";
            $myUser = $db->fetchRow($sql);

            if ( $myUser['login'] > "" ){                                            
                $session->login = $myUser['login'];
                $session->groupe = $myUser['groupe'];                           
            }
            $this->_redirect('/');
        } else {
            $view = Zend::registry('view');
            echo $view->render('userLoginView.php');
        }
    }
    public function disconnectAction()
    {
        $session = Zend::registry('fSession');

        $session->login = "";
        $session->groupe = "";

        $this->_redirect('/');
    }
    public function gestionAction()
    {
        $get = Zend::registry('fGet');
        $db = Zend::registry('db');        

        $myMessage = $get->getRaw('message');

        $sql = "SELECT * FROM users";
        $users = $db->query($sql);

        $view = Zend::registry('view');
        $view->usersListe = $users;
        $view->message = $myMessage;
        echo $view->render('configListUserView.php');
    }
    public function deleteAction()
    {
        $get = Zend::registry('fGet');
        $db = Zend::registry('db');        
        $myLogin = $get->getRaw('login');
        
        if ( $myLogin > "" ) {
            $sql = "DELETE FROM users WHERE login = '" . $myLogin . "'";
            $db->query($sql);
            $myMessage = "User " . $myLogin . " supprimer avec succes";
        } else {
            $myMessage = "Erreur de requete";
        }
        
//         $this->_redirect('/user/gestion?message=' . $myMessage);
        $this->_redirect('/user/gestion');
    }
    public function afficheFormAction()
    {
        $get = Zend::registry('fGet');
        $db = Zend::registry('db');
        $myLogin = $get->getRaw('login');       

        $sql = "SELECT groupe FROM users WHERE login = '" . $myLogin . "'";
        $myGroupe = $db->fetchOne($sql);


        $Groupes = array('guest','staff','editor','admin');
        $myChoice = "";
        foreach ($Groupes as $Groupe) {
            if ( $Groupe == $myGroupe ) $selected = "selected";
            else $selected = "";
            $myChoice .= "<option " . $selected . ">" . $Groupe . "</option>";
        }

        $view = Zend::registry('view');
        $view->login = $myLogin;
        $view->myChoice = $myChoice;
        echo $view->render('configFormUserView.php');
    }
    public function validFormAction()
    {
        $post = Zend::registry('fPost');
        $myLogin = $post->getRaw('login');
        $myPassword = $post->getRaw('password');
        $myConfirm = $post->getRaw('confirm');
        $myGroupe = $post->getRaw('groupe');

        $db = Zend::registry('db');        

        if ( $myLogin > "" ) {
            if ( $myPassword > "" ) {
                if ( $myPassword == $myConfirm ){                   
                    $myPassword = md5($myPassword);

                    $sql = "SELECT login FROM users WHERE login = '".$myLogin."'";
                    if ( $myUser = $db->fetchOne($sql) ) {
                        $sql = "UPDATE users SET password = '" . $myPassword . "', groupe = '" . $myGroupe . "' WHERE login = '" . $myLogin . "'";
                        $db->query($sql);
                        $myMessage = "Mise a jour effectuée avec succes";
                    } else {
                        $sql = "INSERT INTO users (login, password, groupe) VALUES ('" . $myLogin . "', '" . $myPassword . "', '" . $myGroupe . "')";
                        $db->query($sql);
                        $myMessage = "Nouvel utilisateur creer avec succes";
                    }
                    
                } else {
                    $myMessage = "Erreur de saisie du password";
                }
            } else {
                $myMessage = "Vous devez saisir un password";
            }
        } else {
            $myMessage = "Vous n'avez pas saisi de Login";
        }

        //$this->_redirect('/user/gestion?message=' . $myMessage);
        $this->_redirect('/user/gestion');
    }
    public function noRouteAction()
    {
        $this->_redirect('/');
    }
}
?>
